﻿using Dapper_Core.Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace Dapper_Core.Common
{
    public class IPUtility
    {
        /// <summary>
        /// 获取访问者的真实IP
        /// </summary>
        /// <returns></returns>
        public static string GetVisitorRealIP()
        {
            string realIP = "";
            if (string.Equals(Appsettings.DeployType, "NginxCluster", StringComparison.OrdinalIgnoreCase))
            {
                if (!string.IsNullOrEmpty(MyHttpContext.Current?.Request.Headers["X-Real-IP"]))
                {
                    realIP = MyHttpContext.Current.Request.Headers["X-Real-IP"];
                }
                else
                {
                    realIP = MyHttpContext.Current?.Connection.RemoteIpAddress.ToString();
                }
            }
            else
            {
                realIP = MyHttpContext.Current?.Connection.RemoteIpAddress.ToString();
            }
            return realIP;
        }
    }
}
